/*=================================页面事件============================================================*/
//页面的load方法
function loadNews(pageNo){
	queryNewsList(pageNo);
}

//输入页码，跳转到指定页面的操作
function changeRandomPage(){
	var pageCount = document.getElementById("pageCount").value;
	var inputValue = document.getElementById("pageNoInput").value;
	if(!isPositiveInteger(inputValue)){
		alert("请输入一个正整数。");
		pageNoInput.focus();
		pageNoInput.select();
		return;
	}
	if(parseInt(inputValue) > parseInt(pageCount)){
		alert("您输入的页码超过最大页码，请重新输入。");
		pageNoInput.focus();
		pageNoInput.select();
		return
	}
	loadNews(inputValue);
}

//点击全选按钮
function selectAll(){
	var len = ids.length;
	for(var i=0; i < len; i++){
		ids[i].checked = true;
	}
}

//点击全不选按钮
function selectNone(){
	var len = ids.length;
	for(var i=0; i < len; i++){
		ids[i].checked = false;
	}
}

//删除选中的记录
function deleteRows(){
	if (!confirm("确定要删除选中的记录吗？")){
		return;
	}
	var ids = document.getElementsByName("ids");
	var len = ids.length;
	var checked_ids = "";
	for(var i=0; i < len; i++){
		if(ids[i].checked){
			checked_ids += ids[i].value + ",";
		}
	}
	if(bXmlHttpSupport) {
    	var sUrl = 'deleteRows.action';
    	var params = "ids=" + checked_ids;
        var oRequest = new XMLHttpRequest();
        oRequest.onreadystatechange = function() {
        	if(oRequest.readyState == 4) {
        		//alert(oRequest.responseText);
        		loadNews(1);
			}
    	};
    	oRequest.open('POST', sUrl);
    	oRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    	oRequest.send(params);
    }
}

function modifyContent(id){
	var obj = window;
    obj.newsID = id;
	window.open("newsContentModify.jsp?id="+ Math.random());
}

function modifyNews(id){
	var obj = window;
    obj.newsID = id;
	window.open("newsEdit.jsp?id="+ Math.random());
}

/*=================================数据读取============================================================*/

//读取新闻列表
function queryNewsList(pageNo){
	if(bXmlHttpSupport) {
    	var sUrl = 'queryNewsList.action';
    	var params = "pageNo=" + pageNo;
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
        	if(request.readyState == 4) {
            	var oRequest = eval('(' + request.responseText + ')');
            	pageBar.innerHTML = oRequest.pageBar;
            	var newsList = oRequest.newsList;
            	var rowCountPerPage = oRequest.rowCountPerPage;
            	putNewsIntoTable(pageNo, rowCountPerPage, newsList);
            	makeTableColor("newsTable","#f8fbfc","#e5f1f4","#ecfbd4");
			}
    	};
    	request.open('POST', sUrl);
    	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    	request.send(params);
    }
}

/*=================================数据处理============================================================*/
//将读取的新闻列表放入table中
function putNewsIntoTable(pageNo, rowCountPerPage, newsList){
	var rowlength = tableBody.rows.length; 
    for(var i=0;i <rowlength;i++) {
    	tableBody.deleteRow(0); 
    }
	for (var i in newsList){
		var rowObj = document.createElement("tr");
		var j = (pageNo - 1)*rowCountPerPage + new Number(i) + 1;
		var checkboxString = "<input type='checkbox' name='ids' value='"+newsList[i][0]+"'/>";
		addOneTD(rowObj, checkboxString, "");
		addOneTD(rowObj, j, "");
		addOneTD(rowObj, newsList[i][0], "");
		addOneTD(rowObj, newsList[i][1], "");
		addOneTD(rowObj, newsList[i][2], "");
		addOneTD(rowObj, newsList[i][3].substring(0,10), "");
		addOneTD(rowObj, newsList[i][4].substring(0,10), "");
		addOneTD(rowObj, newsList[i][5], "");
		addOneTD(rowObj, "<input type='button' value='修改新闻' onclick='modifyNews("+newsList[i][0]+");'/>&nbsp&nbsp&nbsp<input type='button' value='修改内容' onclick='modifyContent("+newsList[i][0]+");'/>", "");
		tableBody.appendChild(rowObj);
	}
}

/*=================================其他============================================================*/

//给页面的table的行改变颜色
function makeTableColor(obTable, oddColor, evenColor, hoverColor) {
	var t = document.getElementById(obTable).getElementsByTagName("tr");
	for ( var i = 1; i < t.length; i++) {
		t[i].style.backgroundColor = (t[i].sectionRowIndex % 2 == 0) ? oddColor : evenColor;
		t[i].onmouseover = function() {
				this.style.backgroundColor = hoverColor;
		};
		t[i].onmouseout = function() {
				this.style.backgroundColor = (this.sectionRowIndex % 2 == 0) ? oddColor: evenColor;
		};
	}
}
